<?php include("header.php");
if($_SESSION['user_level'] != 1 OR (!$_SESSION['signed_in']))
{
header("location: redirect.php");
}
else
 {
   $hide_footer = -1;
  $result = $db->query("SELECT COUNT(*) FROM " . $table_prefix . "users");
  if (!$result) {
      echo "$l_error_occured";
  } else {
      $qqq += 1;
      $r = $db->fetch_row($result);
      $numrows = $r[0] - 1;
      $rowsperpage = $users_per_page;
      $totalpages = ceil($numrows / $rowsperpage);
      if (isset($_GET['page']) && is_numeric($_GET['page'])) {
          $currentpage = (int)$_GET['page'];
      } else {
          $currentpage = 1;
      }
      if ($currentpage > $totalpages) {
          $currentpage = $totalpages;
      }
      if ($currentpage < 1) {
          $currentpage = 1;
      }
      $offset = ($currentpage - 1) * $rowsperpage;
      $current_page = $page;
      if ($current_page == "") {
          $current_page = 1;
      }
      $range = $max_pagination_link;
      if ($range >= $totalpages) {
          $range = $totalpages;
      }
	  $ascdesc = "ASC";
	  $sort = "LOWER(user_name)";
      $sorting = "name";
		
      if (isset ($_GET['order']))
      {	  
      if (mysql_real_escape_string($_GET['order']) == "") {
          $ascdesc = "DESC";
      }
      if (mysql_real_escape_string($_GET['order']) == "ASC") {
          $ascdesc = "ASC";
      }
      if (mysql_real_escape_string($_GET['order']) == "DESC") {
          $ascdesc = "DESC";
      }
      }
	  if (isset ($_GET['sort']))   
      {	  
      if (mysql_real_escape_string($_GET['sort']) == "") {
          $sort = "LOWER(user_name)";
          $sorting = "name";
      }
      if (mysql_real_escape_string($_GET['sort']) == "name") {
          $sort = "LOWER(user_name)";
          $sorting = "name";
      }
      if (mysql_real_escape_string($_GET['sort']) == "joined") {
          $sort = "user_date";
          $sorting = "joined";
      }
      if (mysql_real_escape_string($_GET['sort']) == "posts") {
          $sort = "user_posts";
          $sorting = "posts";
      }
      if (mysql_real_escape_string($_GET['sort']) == "last_login") {
          $sort = "user_last_login";
          $sorting = "last_login";
      }
	  }
      
      if (isset ($_GET['order']))
	  {
      if (mysql_real_escape_string($_GET['order']) == "") {
          $ascdesc = "ASC";
      }
      if (mysql_real_escape_string($_GET['order']) == "ASC") {
          $ascdesc = "DESC";
      } else {
          $ascdesc = "ASC";
      }
      }

      if ($page <= 0 or $page > $totalpages) {
          $page = 1;
      }
      
      $offset = $users_per_page * ($page - 1);
      //Hard code style... will be fixed!
      $username = "<a style='text-decoration: underline' href='users.php?order=$ascdesc&sort=name'>Username</a>";
      $joined = "<a style='text-decoration: underline' href='users.php?order=$ascdesc&sort=joined'>Joined</a>";
      $posts = "<a style='text-decoration: underline' href='users.php?order=$ascdesc&sort=posts'>Posts</a>";
      $last_login = "<a style='text-decoration: underline' href='users.php?order=$ascdesc&sort=last_login'>Last login</a>";
      
      $qqq += 1;
      $tags = array('{USERNAME}', '{JOINED}', '{POSTS}', '{LAST_LOGIN}');
      $data = array($username, $joined, $posts, $last_login);
      echo str_replace($tags, $data, file_get_contents("./style/$default_style/members.html"));
      $pageTitle = 'Members';
      $result = $db->query("SELECT * FROM " . $table_prefix . "users WHERE user_name !='Anonymus' ORDER BY $sort " . $ascdesc . " LIMIT $offset, $users_per_page");
      
      while ($list = $db->fetch_array($result,'assoc')) {
          $user = $list['user_name'];
          $user = "<a href=edit_user.php?uid=" . $list['user_id'] . ">" . $list['user_name'] . "</a>";
          if ($list['user_id'] == 1) {
              $user = "</b><i>" . $list['user_name'] . "</i>";
          }
          $last_login = date($date_format, strtotime($list['user_last_login']));
          
          if (date("Y", strtotime($list['user_last_login'])) <= "1990") {
              $last_login = "";
          }
          if (date("d", strtotime($list['user_last_login'])) == date("d")) {
              $last_login = "Today at " . date("H:i", strtotime($list['user_last_login'])) . "";
          }
          if ($list['is_banned'] == 1) {
		   $user = "<a href=edit_user.php?uid=" . $list['user_id'] . "><span style = 'color:#AAAAAA'>" . $list['user_name'] . "</span></a>";
		  }
          
		  $view_user = "<a href ='user.php?u=$list[user_id]'>[View]</a>";
          $ban_user = "<a href ='bans.php?uid=$list[user_id]'>[Ban]</a>";
		  $edit_user = "<a href ='edit_user.php?uid=$list[user_id]'>[Edit]</a>";
          $total_users = mysql_num_rows($result);
          $tags = array('{USER}', '{USER_DATE}', '{USER_POSTS}', '{LAST_LOGIN}','{BAN}','[Edit]','[VIEW]');
          $data = array($user, date($date_format, strtotime($list['user_date'])), $list['user_posts'], $last_login,$ban_user,$edit_user,$view_user);
          echo str_replace($tags, $data, file_get_contents("./style/$default_style/members_row.html"));
      }
  }
  echo "<table><tr>";
  if (isset($_GET['page']) && is_numeric($_GET['page'])) {
      $currentpage = (int)$_GET['page'];
  } else {
      $currentpage = 1;
  }
  if ($currentpage > $totalpages) {
      $currentpage = $totalpages;
  }
  if ($currentpage < 1) {
      $currentpage = 1;
  }
  $offset = ($currentpage - 1) * $rowsperpage;

  if ($current_page == "") {
      $current_page = 1;
  }
  $range = $max_pagination_link;
  if ($range >= $totalpages) {
      $range = $totalpages;
  }
  if (isset ($_GET['order']))
  {
  if (mysql_real_escape_string($_GET['order']) == "") {
      $ascdesc = "DESC";
  }
  if (mysql_real_escape_string($_GET['order']) == "ASC") {
      $ascdesc = "ASC";
  }
  if (mysql_real_escape_string($_GET['order']) == "DESC") {
      $ascdesc = "DESC";
  }
  }
  
  echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' users]  &nbsp; &nbsp;';
  if ($currentpage > 1) {
      echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=1'><<</a> ";
      $prevpage = $currentpage - 1;
      echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$prevpage'><</a> ";
  }
  for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
      if (($x > 0) && ($x <= $totalpages)) {
          if ($x == $currentpage) {
              echo " [<b>$x</b>] ";
          } else {
              echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$x'>$x</a> ";
          }
      }
  }
  if ($currentpage != $totalpages) {
      $nextpage = $currentpage + 1;
      echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$nextpage'>></a> ";
      echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?order=$ascdesc&sort=$sorting&page=$totalpages'>>></a> ";
  }
  echo '</td></tr></table>';

 
$pageTitle = "Users";
$pageContents = ob_get_contents ();
ob_end_clean ();
echo str_replace ('<!--TITLE-->', $pageTitle, $pageContents);  	
 }
?>